package com.xingxuecn.demos.service;

import com.xingxuecn.demos.domain.RoleUser;
import com.xingxuecn.demos.domain.User;
import com.xingxuecn.demos.mapper.RoleUserMapper;
import com.xingxuecn.demos.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Kris at 2018/9/26 17:15
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleUserService roleUserService;

    /**
     * 注解开启事物。可以设置传播属性，隔离级别，回归异常等。。。
     * @param user
     * @return
     * @throws Exception
     */
    @Transactional//(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT,rollbackFor = RuntimeException.class)
    public boolean login(User user) throws Exception {
        user = new User();
        user.setNick("修改2");
        user.setId(7);
        userMapper.updateByPrimaryKeySelective(user);
        user.setId(null);
        userMapper.insertSelective(user);
        return true;
    }


    public boolean createUser(User user) {
        createUserDb(user);
        //设一个默认角色
        roleUserService.defaultRoleForUser(user.getId());
        return true;
    }

    /**
     * 测试事物传播，单独拉出一个方法
     * @param user
     */
    private void createUserDb(User user){
        userMapper.insertSelective(user);
    }
}
